package com.hooya.fa.eu.biz.service.impl;


import com.hooya.fa.eu.biz.dao.mapper.MetaProductMapper;
import com.hooya.fa.eu.biz.dao.mapper.Xsjl2303Mapper;
import com.hooya.fa.eu.biz.dao.mapper.stk.StkOrigCollectMapper;
import com.hooya.fa.eu.biz.service.ResetSkuFlagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ResetSkuFlagServiceImpl implements ResetSkuFlagService {

    @Autowired
    private StkOrigCollectMapper stkOrigCollectMapper;

    @Autowired
    private Xsjl2303Mapper xsjl2303Mapper;

    @Autowired
    private MetaProductMapper metaProductMapper;

    /**
     * 重置SKU标记方法
     */
    @Transactional
    public void reset_sku_flag(String startDate, String endDate) {
        try {
            log.info("开始执行重置SKU标记任务，日期范围：{} 到 {}", startDate, endDate);

            // 获取符合条件的SKU列表
            List<String> stkSkuList = stkOrigCollectMapper.selectSkuList(startDate,endDate);

            //这个是在rxh表中找指定时间的sku但是py流程中没有使用到它
//            List<String> rxhSkuList = xsjl2303Mapper.selectSkuList(startDate,endDate);

            // 重置所有needFlag为0
            int resetCount = metaProductMapper.resetNeedFlagToZero();
            log.info("重置了{}条记录的needFlag为0", resetCount);

            // 批量更新needFlag为1
            int count = metaProductMapper.batchUpdateNeedFlagToOne(stkSkuList);
            log.info("批量更新了{}条记录的needFlag为1", count);

            log.info("重置SKU标记任务完成");
        } catch (Exception e) {
            log.error("重置SKU标记任务失败", e);
            throw new RuntimeException("重置SKU标记任务失败", e);
        }
    }
}